package layers;

import gui.Window;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import operations.Lifo;

import com.bbn.openmap.event.MapMouseListener;
import com.bbn.openmap.layer.DemoLayer;
import com.bbn.openmap.omGraphics.OMAction;
import com.bbn.openmap.omGraphics.OMColor;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMPoly;

import components.Arc;
import components.ArcList;
import components.Coordinate;
import components.Node;

public class ArcLayer extends DemoLayer{

	private ArcList arcs;
	private OMGraphic selectedGraphic;
	private Window window;
	
	public ArcLayer(ArcList arcs, Window window){
		super();
		
		this.selectedGraphic = null;
		this.arcs = arcs;
		this.window = window;
	}

	public OMGraphicList init(){
		 OMGraphicList omList = new OMGraphicList();
		 
		 Arc arc = null;
		 
		 for(int i=0; i<arcs.getCount(); i++){
			 arc = arcs.getArcs().get(i);
			 omList.add( createArc(arc.getStartNode(), arc.getEndNode(), arc.getBreakCords(), arc.getBreakCount()) );
		 }
		 
		 return omList;
	}
	
	private OMPoly createArc(Node startNode, Node endNode,
			ArrayList<Coordinate> breakCords, int breakCount) {
		
//		System.out.println("s: "+startNode.getLattitude()+","+startNode.getLongtitude()+" e:"+endNode.getLattitude()+","+endNode.getLongtitude()+" bC:"+breakCount);
//		for(int i=0; i<breakCount; i++){
//			System.out.println(i+": "+breakCords.get(i).getLattitude()+" "+breakCords.get(i).getLongtitude());
//		}
//		System.out.println();
		
		float points[] = null;
		
		if( breakCount > 0 ){
			points = new float[breakCount*2+4];
		}
		else{
			points = new float[4];
		}
		
		points[0] = startNode.getLattitude();
		points[1] = startNode.getLongtitude();
		points[points.length-2] = endNode.getLattitude();
		points[points.length-1] = endNode.getLongtitude();
		
		if(breakCount > 0){
			for(int i=2, j=0; i<=breakCount*2; i+=2, j++){
				points[i] =  breakCords.get(j).getLattitude();
				points[i+1] = breakCords.get(j).getLongtitude();
			}
		}
		
//		for(int i=0;i<points.length;i++){
//			System.out.print(" "+points[i]);
//		}
//		System.out.println("kk");
		
		OMPoly poly = new OMPoly(points, OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_RHUMB);
		poly.setLinePaint(Color.red);
		poly.setFillPaint(OMColor.clear);
		poly.setSelectPaint(Color.orange);
		poly.setIsPolygon(false);
		
		return poly;
	}
	
	public void addArc(Arc arc){
		this.getList().add(createArc(arc.getStartNode(), arc.getEndNode(), arc.getBreakCords(), arc.getBreakCount()));
		repaint();
	}

	public void changeSelectedGraphic(Arc arc) {
		selectedGraphic = this.getSelected().getOMGraphicAt(0);
		if(selectedGraphic != null){
			selectedGraphic = createArc(arc.getStartNode(), arc.getEndNode(), arc.getBreakCords(), arc.getBreakCount());
		}
		repaint();
	}
	
	public void drawingComplete(OMGraphic omg, OMAction action){
		super.drawingComplete(omg, action);
		
//		System.out.println(omg+" --- "+action);
		
		//128 - update
		//4 - delete
		//64 - add
		
		if(action.getValue() == 128){
			window.getSW().addNewEvent(new Lifo(omg, Lifo.editArc));
		}
		else if(action.getValue() == 64){
			window.getSW().addNewEvent(new Lifo(omg, Lifo.delArc));
		}
		else if(action.getValue() == 4){
			window.getSW().addNewEvent(new Lifo(omg, Lifo.addArc));
		}
	}
	
	public int findGraphicPosition(OMPoly poly){
		int pos = -1;
		for(int i=0; i<this.getList().size(); i++){
			if((OMPoly)this.getList().getOMGraphicAt(i) == poly){
				pos = i;
				break;
			}
		}
		return pos;
	}

	public void deleteOMGraphicAt(int i) {
		this.getList().removeOMGraphicAt(i);
		repaint();
	}
	
}
